package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xmy.cultivate.entity.*;
import com.xmy.cultivate.entity.response.PracticeCheckRade;
import com.xmy.cultivate.mapper.PracticePlanMapper;
import com.xmy.cultivate.service.IDictService;
import com.xmy.cultivate.service.IPracticePlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmy.cultivate.service.IPracticeTypeService;
import com.xmy.cultivate.service.IStaffService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 练课进度 服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2024-10-09
 */
@Service
public class PracticePlanServiceImpl extends ServiceImpl<PracticePlanMapper, PracticePlan> implements IPracticePlanService {

    @Autowired
    PracticePlanMapper practicePlanMapper;

    @Autowired
    IPracticeTypeService iPracticeTypeService;

    @Autowired
    IStaffService iStaffService;

    @Autowired
    IDictService iDictService;

    @Override
    public IPage<PracticePlan> getEnterList(IPage<?> page,
                                            Integer yearPart,
                                            Integer quarterNum,
                                            String schoolId,
                                            Integer lessonType,
                                            String yearClassId,
                                            String classTypeId,
                                            String subjectsId,
                                            Integer lessonNumStart,
                                            Integer lessonNumEnd,
                                            Long practiceTypeId,
                                            String teacherName,
                                            Long inTeacherId
    ){

        /*PracticeType practiceType = iPracticeTypeService.getById(practiceTypeId);
        Integer teacherType = practiceType.getTeacherType();
        if (teacherType.equals(1)){
            return practicePlanMapper.getEnterList(page,yearPart,quarterNum,schoolId,lessonType,yearClassId,classTypeId,subjectsId,lessonNumStart,lessonNumEnd,practiceTypeId,teacherName,inTeacherId);
        }else {
            return practicePlanMapper.getEnterForTeacherList(page,yearPart,quarterNum,schoolId,yearClassId,classTypeId,subjectsId,lessonNumStart,lessonNumEnd,practiceTypeId,teacherName,inTeacherId);
        }*/

        return practicePlanMapper.getEnterList(page,yearPart,quarterNum,schoolId,lessonType,yearClassId,classTypeId,subjectsId,lessonNumStart,lessonNumEnd,practiceTypeId,teacherName,inTeacherId);
    }

    @Override
    public IPage<PracticePlan> getEnterDetail(IPage<?> page, Integer yearPart,
                                              Integer quarterNum,
                                              String schoolId,
                                              Integer lessonType,
                                              String yearClassId,
                                              String classTypeId,
                                              String subjectsId,
                                              Integer lessonNumStart,
                                              Integer lessonNumEnd,
                                              Long practiceTypeId,
                                              Long teacherId,
                                              String isFinish
    ){
        /*PracticeType practiceType = iPracticeTypeService.getById(practiceTypeId);
        Integer teacherType = practiceType.getTeacherType();
        if (teacherType.equals(1)){
            return practicePlanMapper.getEnterDetail(page,yearPart,quarterNum,schoolId,lessonType,yearClassId,classTypeId,subjectsId,lessonNumStart,lessonNumEnd,practiceTypeId,teacherId,isFinish);
        }else {
            return practicePlanMapper.getEnterForTeacherDetail(page,yearPart,quarterNum,schoolId,lessonType,yearClassId,classTypeId,subjectsId,lessonNumStart,lessonNumEnd,practiceTypeId,teacherId,isFinish);
        }*/

        return practicePlanMapper.getEnterDetail(page,yearPart,quarterNum,schoolId,lessonType,yearClassId,classTypeId,subjectsId,lessonNumStart,lessonNumEnd,practiceTypeId,teacherId,isFinish);
    }

    @Override
    public Result enterSave(PracticePlan practicePlan){

        if (!practicePlan.getCreatedId().equals(1568138350324350975L)){
            if (!practicePlan.getTeacherId().equals(practicePlan.getCreatedId())){
                return new Result(Code.ERROR,"","录入失败，只能录入自己的数据");
            }
        }

        QueryWrapper<PracticePlan> practicePlanQueryWrapper = new QueryWrapper<>();
        practicePlanQueryWrapper.eq("year_part",practicePlan.getYearPart());
        practicePlanQueryWrapper.eq("quarter_num",practicePlan.getQuarterNum());
        practicePlanQueryWrapper.eq("subjects_id",practicePlan.getSubjectsId());
        practicePlanQueryWrapper.eq("year_class_id",practicePlan.getYearClassId());
        practicePlanQueryWrapper.eq("lesson_type",1);
        practicePlanQueryWrapper.eq("teacher_id",practicePlan.getTeacherId());
        practicePlanQueryWrapper.eq("lesson_num",practicePlan.getLessonNum());
        practicePlanQueryWrapper.eq("class_type_id",practicePlan.getClassTypeId());
        practicePlanQueryWrapper.eq("practice_type_id",practicePlan.getPracticeTypeId());
        practicePlanQueryWrapper.last("limit 1");
        PracticePlan practicePlanInfo = practicePlanMapper.selectOne(practicePlanQueryWrapper);

        PracticeType practiceType = iPracticeTypeService.getById(practicePlan.getPracticeTypeId());
        if (practiceType == null){
            return new Result(Code.ERROR,"","类型不存在");
        }

        //选项
        if (practiceType.getType().equals(2)){
            if (StringUtils.isNotBlank(practicePlan.getOptionValue())){
                if (practicePlan.getOptionValue().equals("取消")){
                    practicePlan.setFinishNum(0);
                    practicePlan.setScore(0);
                    practicePlan.setOptionValue("");
                }else {
                    practicePlan.setFinishNum(1);
                    practicePlan.setScore(this.getScore(practicePlan.getOptionValue()));
                }
            }
        }else {
            if (practicePlan.getFinishNum().equals(1)){
                practicePlan.setScore(1);
            }else {
                practicePlan.setScore(0);
            }
        }

        if(practicePlanInfo==null){
            this.save(practicePlan);
        }else {
            /*if (!practicePlan.getCreatedId().equals(practicePlanInfo.getCreatedId())){
                Staff staff = iStaffService.getOneForAdminid(practicePlanInfo.getCreatedId());
                return new Result(Code.ERROR,"","修改失败，已录数据是"+staff.getName()+"录入，由"+staff.getName()+"修改");
            }*/
            if (practicePlan.getFinishNum().equals(0)){
                practicePlan.setAuditTeacherId(0L);
            }
            practicePlanInfo.setFinishNum(practicePlan.getFinishNum());
            practicePlanInfo.setScore(practicePlan.getScore());
            practicePlanInfo.setAuditTeacherId(practicePlan.getAuditTeacherId());
            practicePlanInfo.setOptionValue(practicePlan.getOptionValue());
            this.updateById(practicePlanInfo);
        }

        return new Result(Code.OK,"","成功");

    }

    private Integer getScore(String levelStr){
        String levelUpper = levelStr.toUpperCase();
        Integer score = 0;
        QueryWrapper<Dict> dictQueryWrapper = new QueryWrapper<>();
        dictQueryWrapper.eq("type",27);
        dictQueryWrapper.eq("name",levelUpper);
        dictQueryWrapper.last("limit 1");
        Dict dict = iDictService.getOne(dictQueryWrapper);
        if (dict != null){
            score = dict.getCount();
        }
        return score;
    }

    @Override
    public IPage<PracticePlan> getCheckList(IPage<?> iPage,
                                            Integer yearPart,
                                            Integer quarterNum,
                                            String yearClassId,
                                            String classTypeId,
                                            String subjectsId,
                                            Integer lessonNumStart,
                                            Integer lessonNumEnd,
                                            Long practiceTypeId,
                                            Long checkTeacherId,
                                            String isCheck,
                                            String starDate,
                                            String endDate
    ){
        return practicePlanMapper.getCheckList(iPage,yearPart,quarterNum,yearClassId,classTypeId,subjectsId,lessonNumStart,lessonNumEnd,practiceTypeId,checkTeacherId,isCheck,starDate,endDate);
    }

    @Override
    public Result batchCheck(PracticePlan practicePlan, Long checkTeacherId){
        UpdateWrapper<PracticePlan> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id",practicePlan.getIdList());
        updateWrapper.set("is_check",practicePlan.getIsCheck());
        updateWrapper.set("audit_teacher_id",checkTeacherId);
        updateWrapper.set("check_date_time", LocalDateTime.now().withNano(0));
        this.update(updateWrapper);
        return new Result(Code.OK,"","成功");
    }

    @Override
    public List<PracticeCheckRade> checkRadeList(
                                                 String yearPart,
                                                 String quarterNum,
                                                 String lessonNumStart,
                                                 String lessonNumEnd,
                                                 String startDate,
                                                 String endDate,
                                                 String type,
                                                 String yearClassId,
                                                 String classTypeId,
                                                 String subjectsId,
                                                 String practiceTypeId,
                                                 String teacherName,
                                                 String lessonType,
                                                 String schoolId){
        return practicePlanMapper.checkRadeList(yearPart,quarterNum,lessonNumStart,lessonNumEnd,startDate,endDate,type,yearClassId,classTypeId,subjectsId,practiceTypeId,teacherName,lessonType,schoolId);
    }

    @Override
    public IPage<PracticeCheckRade> checkRadeDetail(IPage<?> page,
                                                    String yearPart,
                                                    String quarterNum,
                                                    String lessonNumStart,
                                                    String lessonNumEnd,
                                                    String startDate,
                                                    String endDate,
                                                    String type,
                                                    String yearClassId,
                                                    String classTypeId,
                                                    String subjectsId,
                                                    String practiceTypeId,
                                                    String teacherId,
                                                    String lessonType,
                                                    String schoolId,
                                                    String isCheck){
        return practicePlanMapper.checkRadeDetail(page,yearPart,quarterNum,lessonNumStart,lessonNumEnd,startDate,endDate,type,yearClassId,classTypeId,subjectsId,practiceTypeId,teacherId,lessonType,schoolId,isCheck);
    }
}
